<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsPartsController extends PublicController{
	 public function index(){
		 $this->display();
	 }

	 //查询配件列表
	 public function get_parts_list(){
	    $limit = I('get.limit');
        $page = I('get.page');
        $like = I('get.like');
          if($like!=""){
	    	$where="a.id like '%".$like."%' or a.parts_title like '%".$like."%' or a.parts_name like '%".$like."%' or a.parts_moblie like '%".$like."%'";
	    }
	    
	 	$ret['total']=M('goods_parts')->count();
	 	$ret['data']=M('goods_parts')->alias('a')
	 	->where($where)
        ->page($page,$limit)
	 	->select();
	 	$ret['code'] = 200;
        ret($ret);
	 }
	 //点击跳转编辑页面详情
	public function edit(){ 
		$data['id']=I('get.id'); //接受修改配件ID
		$parts=M('goods_parts')->where(['id'=>$data['id']])->find();

		$partsimg=json_decode($parts['parts_img']);
		$imgs=array(); 
		foreach ($partsimg as $key => $value) {
			$iii['imgs_src']=$partsimg[$key];
			$iii['imgs_ressrc']=str_replace('/', '', $partsimg[$key]);
			$iii['imgs_ressrc']=str_replace('.', '', $iii['imgs_ressrc']);
			$iii['imgs_ressrc']=str_replace('_', '', $iii['imgs_ressrc']);
			array_push($imgs, $iii);
		}
		$partszizhi=json_decode($parts['parts_zizhi']);
		$zizhi=array();
		foreach ($partszizhi as $key => $value) {
			$zz['zizhi_src']=$partszizhi[$key];
			$zz['zizhi_ressrc']=str_replace('_', '', $partszizhi[$key]);
			$zz['zizhi_ressrc']=str_replace('/', '', $zz['zizhi_ressrc']);
			$zz['zizhi_ressrc']=str_replace('.', '', $zz['zizhi_ressrc']);
			array_push($zizhi, $zz);
		}

		$this->data=$parts;//配件字段详情
		$this->imgs=$imgs; //产品图片数组
		$this->zizhi=$zizhi;//资质数组
		$this->display();
	}
	//单图上传
    public function partslogo(){
      $name = date("Ymd_His_",time()).rand(100000,999999);
      copy($_FILES['file']['tmp_name'],'Public/upload/parts/partslogo/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['msg'] = "上传成功";
      $ret['src'] = '/Public/upload/parts/partslogo/'.$name.'.jpg';
      ret($ret);
    }

    //产品图上传
    public function partsimg(){
      //ret($_FILES);exit();
      $name = date("Ymd_His_",time()).rand(100000,999999);
          copy($_FILES['file']['tmp_name'],'Public/upload/parts/partsimg/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['msg'] = '上传成功！';
      $ret['data']['src'] = '/Public/upload/parts/partsimg/'.$name.'.jpg';
      ret($ret);
    }
     //资质上传
    public function partszizhi(){
      //ret($_FILES);exit();
      $name = date("Ymd_His_",time()).rand(100000,999999);
          copy($_FILES['file']['tmp_name'],'Public/upload/parts/partszizhi/'.$name.'.jpg');
      $ret['isSuccess'] = 1;
      $ret['msg'] = '上传成功！';
      $ret['data']['src'] = '/Public/upload/parts/partszizhi/'.$name.'.jpg';
      ret($ret);
    }

    //编辑提交
    public function update(){
    	$data['id']=I('post.id');
    	$data['parts_title']=I('post.title');
    	$data['parts_jianxie']=I('post.jianxie');
    	$data['parts_logo']=I('post.logo');
    	$data['parts_img']=json_encode($_POST['imgs']);
    	$data['parts_zizhi']=json_encode($_POST['zizhi']);
    	$data['parts_content']=I('post.content');
    	$data['parts_zhuying']=I('post.zhuying');
    	$data['parts_name']=I('post.name');
    	$data['QQ']=I('post.QQ');
    	$data['parts_wechar']=I('post.wechar');
    	$data['parts_pgone']=I('post.pgone');
    	$data['parts_moblie']=I('post.moblie');
    	$data['parts_address']=I('post.address');
    	$data['parts_sort']=I('post.sort');
    	$data['parts_hot']=I('post.hot');
    	$re=M('goods_parts')->where(['id'=>$data['id']])->save($data);
    	if($re!==false){
    		$ret['isSuccess']=1;
    		$ret['msg']="修改成功";
    		ret($ret);
    	}else{
    		$ret['isSuccess']=0;
    		$ret['msg']="修改失败";
    		ret($ret);
    	}
    }

    //添加配件
    public function add(){
    	$this->display();
    }

    //添加提交
    public function insert(){
    	$data['parts_title']=I('post.title');
    	$data['parts_jianxie']=I('post.jianxie');
    	$data['parts_logo']=I('post.logo');
    	$data['parts_img']=json_encode($_POST['imgs']);
    	$data['parts_zizhi']=json_encode($_POST['zizhi']);
    	$data['parts_content']=I('post.content');
    	$data['parts_zhuying']=I('post.zhuying');
    	$data['parts_parts_name']=I('post.name');
    	$data['QQ']=I('post.QQ');
    	$data['parts_wechar']=I('post.wechar');
    	$data['parts_pgone']=I('post.pgone');
    	$data['parts_moblie']=I('post.moblie');
    	$data['parts_address']=I('post.address');
    	$data['parts_sort']=I('post.sort');
    	$data['parts_hot']=I('post.hot');
    	$re=M('goods_parts')->add($data);
    	if($re){
    		$ret['isSuccess']=1;
    		$ret['msg']="添加成功";
    		ret($ret);
    	}else{
    		$ret['isSuccess']=0;
    		$ret['msg']="添加失败";
    		ret($ret);
    	}
    }

}
